package cn.wolfcode.crm.service.Impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.query.CustomerChartQO;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.vo.CustomerChartVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
@Transactional
public class CuotomerSreviceImpl implements ICustomerService{

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public void save(Customer customer) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        customer.setInputUser(employee);
        customer.setSeller(employee);
        customer.setInputTime(new Date());
        customerMapper.insert(customer);
    }

    @Override
    public void delete(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    @Override
    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> list() {
        return customerMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> customers = customerMapper.selectForList(qo);
        return new PageInfo(customers);
    }

    @Override
    public void updateStatus(Long cid, int status) {
        if(status==Customer.STATUS_FORMAL){

            customerMapper.updateStatus(cid,status,new Date());
        }else {
            customerMapper.updateStatus(cid,status,null);

        }
    }

    @Override
    public List<CustomerChartVO> queryCustomerChart(CustomerChartQO qo) {
        return customerMapper.selectCustomerChart(qo);
    }
}
